﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using GameLogic.Logic;

#if WINDOWS_PHONE

using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

#endif

namespace GameLogic.Controls
{
    public class GameContainer : Control
    {
        // Constructor
        public GameContainer()
        {
            this.DefaultStyleKey = typeof(GameContainer);

            this.Loaded += (s, e) =>
                {
                    NavigationContext = Helper.GetNavigationContext(this);
                    NavigationService = Helper.GetNavigationService(this);
                    NavigationService.Navigate(Const.MenuPageUri);
                };
        }

        public static NavigationContext NavigationContext;
        public static NavigationService NavigationService;

        private static int _lastPoints = -1;
        public static int LastPoints
        {
            get { return GameContainer._lastPoints; }
            set { GameContainer._lastPoints = value; }
        }

        private static string _lastUserName = string.Empty;
        public static string LastUserName
        {
            get { return GameContainer._lastUserName; }
            set { GameContainer._lastUserName = value; }
        }

        private void InitGameContainer()
        {
            if (DesignerProperties.IsInDesignTool)
            {
                return;
            }

            if (_loaded)
            {
                return;
            }

            GameControlStatic = this.GameControl;
            Game = this.GameControl as IGameFeatures;

            // TODO: Company Name in IGameFeatures
            Storage.Instance.CompanyName = Game.GameName;

            // TODO: Developer Name in IGameFeatures
            Storage.Instance.DeveloperName = Game.GameName;

            Storage.Instance.GameName = Game.GameName;

            //if (gameContent != null)
            //{
            //    gameContent.Content = this.GameControl;

            //    if (this.GameControl == null)
            //    {
            //        MessageBox.Show("No game content defined!", "Warning", MessageBoxButton.OK);
            //    }
            //    else
            //    {
            //        this.Game = this.GameControl as IGameFeatures;

            //        GameControlStatic = this.GameControl;

            //        //if (this.Game == null)
            //        //{
            //        //    MessageBox.Show("Game content does not implement IGameFeatures!", "Warning", MessageBoxButton.OK);
            //        //    return;
            //        //}
            //        //else
            //        //{
            //        //    // Register Events to change game properties and handle game property change events
            //        //    this.Game.ChangeLivesFired += (s, e) => 
            //        //        {
            //        //            lives.Add(e.Value);
            //        //        }; 
            //        //    this.Game.ChangeScoresFired += (s, e) =>
            //        //        {
            //        //            scores.Add(e.Value);
            //        //        }; 
            //        //    this.Game.GameOverFired += (s, e) =>
            //        //        {
            //        //            OnGameOver();
            //        //        }; 
            //        //}
            //    }
            //}

            // Listen for control events
            //timer.TimerDefeat += new EventHandler(timer_TimerDefeat);
            //lives.LivesDefeat += new EventHandler(lives_LivesDefeat);
            //scores.ScoresDefeat += new EventHandler(scores_ScoresDefeat);
            //scores.ScoresVictory += new EventHandler(scores_ScoresVictory);    

            // Properties sich selber zuweisen, weil jetzt alle Controls fertig da sein sollten.
            //this.CurrentGameContainerState = GameContainerState.StartScreen;

            // Hide menu buttons if applicable
            //if (!this.SettingsVisible && settingsButton != null)
            //    settingsButton.Visibility = Visibility.Collapsed;

            //if (!this.HighscoresVisible && highscoreButton != null)
            //    highscoreButton.Visibility = Visibility.Collapsed;

            //_startScreenHeader.Text = this.Game.GameName;

            _loaded = true;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            NavigationService = Helper.GetNavigationService(this);

            InitGameContainer();

            _loaded = true;
        }

        bool _loaded = false;

        #region  Properties

        //const string C_StartScreenHeaderTextBlockName = "_startScreenHeader";

        //const string C_GameContentName = "_gameContent";
        //const string C_GameButtonName = "_startButton";
        //const string C_SettingsButtonName = "_settingsButton";
        //const string C_HighscoreButtonName = "_highscoreButton";
        //const string C_GameBackButtonName = "_gameBackButton";
        //const string C_GamePauseButtonName = "_gamePauseButton";
        //const string C_GameContinueButtonName = "_gameContinueButton";
        //const string C_SettingsBackButtonName = "_settingsBackButton";
        //const string C_HighscoreBackButtonName = "_highscoreBackButton";
        //const string C_TimerControlName = "_timer";
        //const string C_LifeControlName = "_lives";
        //const string C_ScoreControlName = "_scores";
        //const string C_HighscoreControlName = "_highscores";

        //Button startButton;
        //Button settingsButton;
        //Button highscoreButton;
        //Button gameBackButton;
        //Button gamePauseButton;
        //Button gameContinueButton;
        //Button gameStartButton;
        //Button gameNewButton;
        //Button settingsBackButton;
        //Button highscoreBackButton;

        //TextBlock _startScreenHeader;
        //ContentControl gameContent;
        //TimerControl timer;
        //LifeControl lives;
        //ScoreControl scores;
        //HighscoreControl highscores;

        GameContainerState currentGameContainerState;
        
        #endregion

        #region Public and Dependency Properties

        public static FrameworkElement GameControlStatic;

        [Category("GameContainer")]
        [Description("Gibt das FrameworkElement an, in dem die eigentliche Spiellogik enthalten ist.")]
        public FrameworkElement GameControl
        {
            get { return (FrameworkElement)GetValue(GameControlProperty); }
            set { SetValue(GameControlProperty, value); }
        }

        public static readonly DependencyProperty GameControlProperty =
            DependencyProperty.Register(
            "GameControl",
            typeof(FrameworkElement),
            typeof(GameContainer),
            new PropertyMetadata(null));

        [Category("GameContainer")]
        public bool SettingsVisible
        {
            get { return (bool)GetValue(SettingsVisibleProperty); }
            set { SetValue(SettingsVisibleProperty, value); }
        }

        public static readonly DependencyProperty SettingsVisibleProperty =
            DependencyProperty.Register(
                "SettingsVisible",
                typeof(bool),
                typeof(GameContainer),
                new PropertyMetadata(true, null));

        [Category("GameContainer")]
        public bool HighscoresVisible
        {
            get { return (bool)GetValue(HighscoresVisibleProperty); }
            set { SetValue(HighscoresVisibleProperty, value); }
        }

        public static readonly DependencyProperty HighscoresVisibleProperty =
            DependencyProperty.Register(
                "HighscoresVisible",
                typeof(bool),
                typeof(GameContainer),
                new PropertyMetadata(true, null));

        [Category("GameContainer")]
        public bool GameReadyVisible
        {
            get { return (bool)GetValue(GameReadyVisibleProperty); }
            set { SetValue(GameReadyVisibleProperty, value); }
        }

        public static readonly DependencyProperty GameReadyVisibleProperty =
            DependencyProperty.Register(
                "GameReadyVisibleProperty",
                typeof(bool),
                typeof(GameContainer),
                new PropertyMetadata(true, null));

        [Category("GameContainer")]
        public GameContainerState CurrentGameContainerState
        {
            get { return currentGameContainerState; }
            set
            {
                currentGameContainerState = value;
                //GotoState(value);
                //VisualStateManager.GoToState(this, this.CurrentGameContainerState.ToString(), false);
            }
        }

        private static IGameFeatures _game;
        public static IGameFeatures Game
        {
            get { return GameContainer._game; }
            set { GameContainer._game = value; }
        }

        #endregion

        void StopGame()
        {
            if (Game != null)
            {
                Game.Stop();
            }
        }
    }
}
